/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/universe_observers.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"
#include "PlanetWars2dRT-core/specific/model/planet_observers.h"


EXTERN_C_BEGIN


bool
universe_add_observer(universe_t* a_universe,
		      const observer_t* an_observer)
{
  return observers_add(&a_universe->observers, an_observer);
}

bool
universe_add_observer_to_planets(universe_t* a_universe,
				 const observer_t* an_observer,
				 bool exit_on_error)
{
  bool all_added = true;
  const size_t nb_planets = universe_get_nb_planets(a_universe);
  for(size_t i=0; i < nb_planets; ++i)
    {
      if(!planet_add_observer(universe_get_planet_no_check(a_universe, i),
			      an_observer))
	{
	  if(exit_on_error)
	    {
	      return false;
	    }
	  all_added = false;
	}
    }
  return all_added;
}

bool
universe_remove_observer_without_order(universe_t* a_universe,
				       const observer_t* an_observer)
{
  return observers_remove_without_order(&a_universe->observers, an_observer);
}

bool
universe_remove_observer_with_order(universe_t* a_universe,
				    const observer_t* an_observer)
{
  return observers_remove_with_order(&a_universe->observers, an_observer);
}

void
universe_remove_observers(universe_t* a_universe)
{
  observers_clear(&a_universe->observers);
}

void
universe_notify_observers(universe_t* a_universe, void* data)
{
  observers_notify(&a_universe->observers, a_universe, data);
}

size_t
universe_get_nb_observers(const universe_t* a_universe)
{
  return a_universe == NULL ? 0 : observers_get_nb(&a_universe->observers);
}


EXTERN_C_END
